package com.koala.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class KoalaDB { 

    private static final String TAG = "KoalaDB";       
    private static final String DATABASE_NAME = "Koala.db";    
    private static final int DATABASE_VERSION = 1;
	
	/* Table Dog */
	public static final String DOG_TABLE_NAME = "Dog";
	public static final String DOG_COLUMN_NAME = "dog_name";
	public static final String DOG_COLUMN_BREED = "dog_breed";
	
	private static final String DATABASE_CREATE_DOG =
	        "create table Dog (person_phone integer not null, dog_name text not null, dog_breed text not null, "
	        + "primary key(person_phone, dog_name));";
	
	/* Table Person */
	public static final String PERSON_TABLE_NAME = "Person";
	public static final String PERSON_COLUMN_NAME = "person_name";
	public static final String PERSON_COLUMN_PHONE = "person_phone";
	
	private static final String DATABASE_CREATE_PERSON =
            "create table Person (person_phone integer primary key, person_name text not null);";
	
	/* Table Service */
	public static final String SERVICE_TABLE_NAME = "Service";
	public static final String SERVICE_COLUMN_DESCRIPTION = "service_description";
	public static final String SERVICE_COLUMN_DATE = "service_date";
	public static final String SERVICE_COLUMN_PRICE = "service_price";

	private static final String DATABASE_CREATE_SERVICE =
            "create table Service (person_phone integer not null, dog_name text not null, service_date text not null, service_price real not null, service_description text not null, "
            + "primary key(person_phone, dog_name, service_date));";


    private Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    
    public KoalaDB(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE_DOG);
            db.execSQL(DATABASE_CREATE_PERSON);
            db.execSQL(DATABASE_CREATE_SERVICE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                              int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                  + " to "
                  + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS Dog");
            db.execSQL("DROP TABLE IF EXISTS Person");
            db.execSQL("DROP TABLE IF EXISTS Service");
            onCreate(db);
        }
    }


    public SQLiteDatabase open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return db;
    }

       
    public void close() 
    {
        DBHelper.close();
    }


    public long insertTitle(ContentValues initialValues,String TableName) 
    {


        return db.insert(TableName, null, initialValues);
    }
}
